<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生取药</title>
    <link rel="stylesheet" href="../css/main.css" media="all">
    <link rel="stylesheet" href="../css/bootstrap.css">
    <script type="text/javascript" src="../js/jquerysession.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
</head>

<body>

<div class="container-fluid">
    <div class="row-fluid">
        <div class="col-md-12">
            <table class="layui-table">
                <thead>
                <tr>
                    <th>
                        编号
                    </th>
                    <th>
                        学生姓名
                    </th>
                    <th>
                        诊断结果
                    </th>

                    <th>
                        配药情况
                    </th>

                    <th>
                        确认发放
                    </th>

                </tr>
                </thead>
                <tbody id="patientTab">
                <tr>
                    <td>
                        1
                    </td>
                    <td>
                        TB - Monthly
                    </td>
                    <td>
                        01/04/2012
                    </td>
                    <td>
                        Default
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

</body>


<script>

    $(function () {
        $.ajax({
                url:"/diagnosis/findAll",
                type:"POST",
                data: null,
                // async: false,

                success: function(result){
                    if (result.state == 200){
                        var dia = result.data;
                        $("#patientTab").html('');
                        $.each(dia,function (i) {
                            $tr = $("<tr/>");

                            $td1 = $("<td/>").text(dia[i].cr_ID);

                            var studentID;
                            var studentName;
                            var cr_State;
                            $.ajax({
                                url:"/clinicReg/findById",
                                type:"POST",
                                async: false,
                                data: {
                                    cr_ID:dia[i].cr_ID
                                },

                                success:function (rst) {
                                    if(rst.state==200){
                                        studentID = rst.data.st_ID;
                                        cr_State = rst.data.cr_State;
                                    }
                                }
                            });
                            if (cr_State != 2){
                                return true;
                            }

                            $.ajax({
                                url:"/student/findByID",
                                type:"POST",
                                async: false,
                                data: {
                                    st_ID:studentID,
                                },
                                success:function (rst) {
                                    if(rst.state==200){
                                        studentName = rst.data.realName;

                                    }
                                }
                            });
                            $td2 = $("<td/>").text(studentName);
                            $td3 = $("<td/>").text(dia[i].dg_info);
                            var med_list;
                            $.ajax({
                                url:"/diagnosis/findByCr_Id",
                                type:"POST",
                                async: false,
                                data: {
                                    cr_ID:dia[i].cr_ID,
                                },
                                success:function (rst) {
                                    if(rst.state==200){
                                        med_list = rst.data.med_ID_List;
                                    }
                                }
                            });

                            var med_list_scr = [];
                            $.ajax({
                                url:"/medicine/findById_List",
                                type:"POST",
                                async: false,
                                data: {
                                    id_list:med_list,
                                },
                                success:function (rst) {
                                    if(rst.state==200){
                                        for(var i =0;i<(med_list.length+1)/2;i++){
                                            med_list_scr.push(rst.data[i].med_Name);


                                        }
                                    }
                                }
                            })

                            console.log(med_list_scr)
                            $td4 = $("<td/>").text(med_list_scr);

                            $td5 = $("<button/>").text("确认发放").addClass("btn btn-primary col-md-6").click(function () {
                                $.ajax({
                                    url:"/clinicReg/updateState",
                                    type:"POST",
                                    async: false,
                                    data: {
                                        cr_State:1,
                                        cr_ID:dia[i].cr_ID
                                    },success:function (rst) {
                                        if (rst.state==200){
                                            location.reload();
                                        }
                                    }
                                })
                            });

                            $tr.append($td1).append($td2).append($td3).append($td4).append($td5);

                            $("#patientTab").append($tr);
                        });


                    }


                }
            }
        );
    })

</script>
</html>